package org.dawn.deveco.plugin.resourceeditor.component;

import org.dawn.deveco.plugin.resourceeditor.utils.Logger;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class JExtensionPanelAdaptor {
    private static final Logger LOG = Logger.getInstance(JExtensionPanelAdaptor.class);

    private final Container parent;
    private final java.util.List<Component> components = new ArrayList<>();
    private final java.util.List<Container> containers = new ArrayList<>();

    public JExtensionPanelAdaptor(Container parent) {
        this.parent = parent;
    }

    /**
     * 使用参数构造JExtensionPanel对象，并且自动添加到父容器中，并以BorderLayout层层级联的方式布局。
     * 注意：会更改父容器的布局方式为BorderLayout
     * @param title  面板标题
     * @param content 面板内容
     * @param expand 是否展开
     * @return this
     */
    public JExtensionPanelAdaptor add(String title, JExtensionPanel.ActionButtonParam[] actionButtons, Component content, boolean expand) {
        if (parent == null) {
            throw new IllegalArgumentException("parent container is null");
        }

        JExtensionPanel extensionPanel = new JExtensionPanel(title, actionButtons, content, expand);
        components.add(extensionPanel);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(extensionPanel, BorderLayout.NORTH);

        if (containers.size() > 0) {
            JPanel prevPanel = (JPanel) containers.get(containers.size() - 1);
            prevPanel.add(panel, BorderLayout.CENTER);
        }
        containers.add(panel);

        if (components.size() == 1) {
            this.parent.setLayout(new BorderLayout());
            this.parent.add(panel, BorderLayout.CENTER);
        }

        return this;
    }

    public JExtensionPanelAdaptor add(String title, Component content) {
        return add(title, null, content, false);
    }

}
